# http://br.shys-edu.com/
# 电影资源网
import os
import re
import shutil

import requests
from lxml import etree

from util.headers import get_headers_init_user_agent

root = 'static/电影资源网/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)
url = 'http://br.shys-edu.com/'
response = requests.get(url)
# print(res.status_code)
content = response.content.decode('utf-8')
# print(content)
tree =etree.HTML(content)
titles = tree.xpath('//div//ul[1]/li/div[1]/p[1]/a[1]')
items = tree.xpath('//li/a/@data-original')
for item,title in zip(items,titles):
    # print(item)
    thumb_url = f'http://br.shys-edu.com{item}'
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': url
    })
    thumb_response = requests.get(thumb_url, headers=headers)
    with open(f"{root}/{title.text}_thumb.jpg", "wb") as f:
        f.write(thumb_response.content)